function [p,ai] = prmat(sd,md,id,rv,cpn,yld,basis)
%PRMAT  Price with interest at maturity.
%   Returns the price p and accrued interest AI of a security that pays
%   interest at maturity.  This function also applies to zero-coupon bonds
%   or pure discount securities by letting CPN = 0.
%
%   [P,AI] = PRMAT(SD,MD,ID,RV,CPN,YLD)
%   [P,AI] = PRMAT(SD,MD,ID,RV,CPN,YLD,BASIS)
%
%   Enter dates as serial date numbers or date strings.
%
%   Inputs:
%       SD      -  Settlement date
%       MD      -  Maturity date
%       ID      -  Issue date
%       RV      -  Par value
%       CPN     -  Coupon rate
%       YLD     -  Annual yield
%
%   Optional Inputs:
%       Basis   -  the day-count basis:
%                  0 = actual/actual (default)
%                  1 = 30/360 SIA
%                  2 = actual/360
%                  3 = actual/365
%                  4 = 30/360 PSA
%                  5 = 30/360 ISDA
%                  6 = 30E/360
%                  7 = actual/365 Japanese
%                  8 - actual/actual ISMA
%                  9 - actual/360 ISMA
%                 10 - actual/365 ISMA
%                 11 - 30/360 ISMA
%                 12 - actual/365 ISDA
%                 13 - bus/252
%
%   Using the following data,
%
%       SD = '02/07/1992'
%       MD = '04/13/1992'
%       id = '10/11/1991'
%       RV = 100
%       CPN = 0.0608
%       YLD = 0.0608
%       BASIS = 1
%
%   [p,ai] = prmat(sd,md,id,rv,cpn,yld,basis)
%
%   returns p = 99.98 and ai = 1.96
%
%   See also YLDMAT, PRBOND, PRDISC.
%
%   Reference: Mayle, Standard Securities Calculation Methods, Volumes
%              I-II, 3rd edition.  Formula 4.

% Copyright 1995-2006 The MathWorks, Inc.
% $Revision: 1.7.2.8 $   $Date: 2009/05/07 18:23:32 $

if nargin < 7
    basis = zeros(size(rv));
end

if nargin < 6
    error('finance:prmat:notEnoughInputs',...
        'Missing one of SD, MD, ID, RV, CPN, and YLD.');
end

[cpn, sd, md, ~, basis, ~, id, ...
    ~, ~, ~, ~] = ...
    instargbond(cpn,sd,md,[],basis,[],id,[],[],[],rv);

[sd,md,id,rv,~,cpn,basis] = ...
    finargsz(1,sd,md,id,rv,cpn,yld,basis);

% accrued interest
ai = reshape(yearfrac(id,sd,basis).*cpn(:).*rv(:),size(rv));
% Formula 4
p = reshape((rv(:)+(yearfrac(id,md,basis).*cpn(:).*rv(:)))./ ...
    (1+(yearfrac(sd,md,basis).*yld(:)))-ai(:),size(rv));